package com.example.nequimessage;

import android.app.Notification;

import android.os.Bundle;

import android.service.notification.NotificationListenerService;

import android.service.notification.StatusBarNotification;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class NotificationMonitorService extends NotificationListenerService {

            // 在收到消息时触发

            @Override

            public void onNotificationPosted(StatusBarNotification sbn) {

                    // TODO Auto-generated method stub

                     Bundle extras = sbn.getNotification().extras;

                     // 获取接收消息APP的包名

                     String notificationPkg = sbn.getPackageName();

                     // 获取接收消息的抬头

                     String notificationTitle = extras.getString(Notification.EXTRA_TITLE);

                     // 获取接收消息的内容

                     String notificationText = extras.getString(Notification.EXTRA_TEXT);

                        // com.nequi.MobileApp
                    Log.i("11111111111111111111111", System.currentTimeMillis() +" " + notificationPkg);
                    Log.i("NotificationInfo", " Notification posted " + notificationTitle +" & " + notificationText);
                    if(notificationPkg.equals("com.nequi.MobileApp")){
                        System.out.println(222222);
                        String[] arr = notificationText.split(",");
                        String[] arrA = arr[0].split(" ");

                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
//                                String result = HttpUtils.getJsonContent("https://api.apiopen.top/api/sentences");

                                String amount  = arrA[4];
                                String timestr = String.valueOf(System.currentTimeMillis()/1000);
                                String key = "kh9odGdwpGc9iTCcA0KX1hFXzqCXWZyI";
                                String sign = "amount="+amount+"&timestr="+timestr+key;

                                HashMap<String, Object> datas = new HashMap<>();
                                datas.put("amount", amount);
                                datas.put("timestr", timestr);
                                datas.put("sign", HttpUtils.md5(sign));

                                System.out.println(timestr+arrA[4]);
                                String tmd = HttpUtils.doPostWithParams("https://www.quickflower.cc/api/webhook", datas);

                                try {
                                    JSONObject jsonObject = new JSONObject(tmd);
                                    Log.i("GETjson", "123456789" + jsonObject);
                                    System.out.println("jsonObject的数据类型是：" + jsonObject.get("code"));

                                    String data =timestr+notificationText;
                                    try {
                                        File file = new File("/sdcard/nequi.txt"); // 设置保存路径及文件名
                                        if (!file.exists()) {
                                            // 如果文件不存在则创建新文件
//                                            file.createNewFile();
                                            if (file.createNewFile()){
                                                System.out.println("File is created!");
                                                //Runtime.getRuntime().exec("chmod 777 /home/test3.txt");
                                                file.setExecutable(true);//设置可执行权限
                                                file.setReadable(true);//设置可读权限
                                                file.setWritable(true);//设置可写权限
                                                System.out.println("is execute allow : " + file.canExecute());
                                                System.out.println("is read allow : " + file.canRead());
                                                System.out.println("is write allow : " + file.canWrite());
                                            }else{
                                                System.out.println("File already exists.");
                                            }
                                        }

                                        FileWriter writer = new FileWriter(file, true);
                                        writer.write(data+"\r\n"); // 写入数据
                                        writer.close(); // 关闭FileWriter对象

                                        System.out.println("数据已成功保存到文件！");
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }
                        });
                        thread.start();
                        try {
                            thread.join();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }else{
                        System.out.println("包名不同");
                    }

            }

            // 在删除消息时触发

            @Override

            public void onNotificationRemoved(StatusBarNotification sbn) {

                        // TODO Auto-generated method stub

                         Bundle extras = sbn.getNotification().extras;

                         // 获取接收消息APP的包名

                         String notificationPkg = sbn.getPackageName();

                         // 获取接收消息的抬头

                         String notificationTitle = extras.getString(Notification.EXTRA_TITLE);

                         // 获取接收消息的内容

                         String notificationText = extras.getString(Notification.EXTRA_TEXT);

                         Log.i("NotificationInfo", " Notification removed " + notificationTitle +" & " + notificationText);

            }

        }
